<?php

namespace App\Jobs;

use App\Models\User;
use DB;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Mail;
use App\Jobs\CustomMail;

class SendEmail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
    /**
     * Create a new job instance.
     *
     * @return void
     */
    private $user;
    private $content;
    private $to;
    private $subject;
    private $type;
    public function __construct($user, $content, $to, $subject,$type)
    {
        $this->user = $user;
        $this->content = $content;
        $this->to = $to;
        $this->subject = $subject;
        $this->type = $type;
    }
    
    /**
     * 执行队列的方法 比如发送邮件
     *
     * @return void
     */
    public function handle()
    {
        \Log::info('邮件send开始发送');
        $userInfo = $this->user;
        if($this->type==1){
            $userInfo->company_id = $userInfo->appcompany_id;
        }
        \Log::info('33'.json_encode($userInfo));
        $email = DB::table('email_sets')
            ->where('company_id', $userInfo->company_id)
            ->first();
        if(empty($email) || (!empty($email) && empty($email->user_name))){
            return response()->json(['code' => 400, 'msg' => '邮箱未配置', 'data' =>""]);
        }
        Mail::to($this->to)->send(new CustomMail($this->user,$this->subject,$this->content,$this->type));
//      Mail::send('emails.test', ['content' => $content],
//            function ($message) use ($address,$name,$to, $subject) {
//                $message->from($address,$name);
//                $message->to($to);
//                $message->subject($subject);
//                \Log::info('邮件发送成功');
//            });
    }
}
